import 'dart:convert';
import 'dart:math';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screen_util.dart';
import 'package:study_online/entity/Problem.dart';
import 'package:study_online/global/colors.dart';
import 'package:study_online/global/myIcons.dart';
import '../DiscussModel.dart';
import 'ImgsContainer.dart';
import 'ProblemReplyItem.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        //appBar: AppBar(
        //  backgroundColor: Colors.black26,
        //  title: Text('container'),
        //),
        body: ProblemDetailPage(),
      ),
    );
  }
}

class ProblemDetailPage extends StatefulWidget {
  ProblemDetailPage({Key key, this.problem}) : super(key: key);
  Problem problem;

  @override
  _ProblemDetailPageState createState() {
    return _ProblemDetailPageState();
  }
}

class _ProblemDetailPageState extends State<ProblemDetailPage> {
  Problem _problemReply = new Problem(
      "windChaser",
      "1",
      "讲的真好",
      12,
      3,
      "2021-10-9",
      "https://img2.baidu.com/it/u=3172022227,2332429611&fm=26&fmt=auto");
  Problem _problem = new Problem(
      "windChaser",
      "1",
      "#算法#flutter#回溯法 flutter用起来真舒服",
      12,
      3,
      "2021-10-9",
      "https://img2.baidu.com/it/u=172917783,2425047487&fm=26&fmt=auto");
  ProblemList _replyList = new ProblemList([]);
  List<String> _labelList = [];
  List<String> _imgUrl = [];

  void _loadData() async {
    Response response = await getComment(widget.problem.postId);
    print("###" + widget.problem.postId.toString());
    print("###" + response.toString());
    var responseData = json.decode(response.toString());
    setState(() {
      _replyList = ProblemList.formJson(responseData);
    });
  }

  @override
  void initState() {
    super.initState();
    _problem = widget.problem;
    _loadData();
  }

  Widget _labelWidget(String name) {
    return Container(
      alignment: Alignment.center,
      height: ScreenUtil().setHeight(54),
      //width: 90,
      margin: EdgeInsets.only(right: 7),
      decoration: BoxDecoration(
        color: Color(0xffF5C5A8),
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(ScreenUtil().setHeight(27)),
          bottomRight: Radius.circular(ScreenUtil().setHeight(27)),
          topRight: Radius.circular(ScreenUtil().setHeight(27)),
        ),
      ),
      padding: EdgeInsets.only(left: 6, right: 6),
      child: Text(
        '#$name',
        style: TextStyle(
          fontSize: ScreenUtil().setSp(31),
          color: Colors.white,
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
      BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: MediaQuery.of(context).size.height),
      designSize: Size(1080, 2340),
    );
    if (_replyList == null) {
      return Container(
        width: 0,
        height: 0,
      );
    } else {
      // for (int i = 0; i < 5; ++i)
      //   _replyList.data.add(_problemReply);
      String tp = _problem.content;
      int num = 0;
      var pos = [0, 0];
      var temp = "";
      for (int i = 0; i < tp.length; ++i) {
        if (tp[i] == '#' || tp[i] == ' ') {
          if (i != 0) _labelList.add(temp);
          temp = "";
        } else
          temp += tp[i];
        if (tp[i] == ' ') {
          _problem.content = tp.substring(i + 1);
          break;
        }
      }
      int pos1 = _problem.content.indexOf("@@");
      if (pos1 != -1) {
        String tp1 = _problem.content.substring(pos1 + 2);
        _problem.content = _problem.content.substring(0, pos1);
        _imgUrl = tp1.split("@@");
        // print(_imgUrl);
      }
      return SafeArea(
        child: Stack(
          children: [
            CustomScrollView(
              slivers: [
                SliverAppBar(
                  title: Text(
                    '问题详情',
                    style: TextStyle(color: Colors.black),
                  ),
                  backgroundColor: Colors.white,
                  leading: IconButton(
                    icon: Icon(
                      Icons.arrow_back,
                      color: Colors.black,
                    ),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  ),
                  floating: true,
                  snap: true,
                ),
                SliverList(
                  delegate: SliverChildListDelegate(
                    [
                      Container(
                        width: ScreenUtil().screenWidth -
                            ScreenUtil().setWidth(110),
                        margin: EdgeInsets.only(
                          top: ScreenUtil().setHeight(58),
                          left: ScreenUtil().setWidth(55),
                          right: ScreenUtil().setWidth(55),
                        ),
                        child: Column(
                          //mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                ClipOval(
                                  child: Image.network(_problem.photo,
                                      width: ScreenUtil().setSp(126),
                                      height: ScreenUtil().setSp(126),
                                      fit: BoxFit.cover),
                                ),
                                SizedBox(width: ScreenUtil().setWidth(13)),
                                Container(
                                  height: ScreenUtil().setHeight(140),
                                  margin: EdgeInsets.only(
                                      left: ScreenUtil().setWidth(20)),
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceAround,
                                    children: [
                                      Text(
                                        _problem.name,
                                        style: TextStyle(
                                          fontSize: ScreenUtil().setSp(46),
                                          color: Colors.black,
                                          fontWeight: FontWeight.bold,
                                        ),
                                      ),
                                      Text(
                                        _problem.time.substring(0,16),
                                        style: TextStyle(
                                          fontSize: ScreenUtil().setSp(40),
                                          color: AppColors.subtext,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                            Container(
                              margin: EdgeInsets.only(
                                  //left: ScreenUtil().setWidth(98),
                                  top: ScreenUtil().setHeight(46)),
                              child: Column(
                                //mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.start,
                                    children: _labelList
                                        .asMap()
                                        .keys
                                        .map((index) =>
                                            _labelWidget(_labelList[index]))
                                        .toList(),
                                  ),
                                  Align(
                                    alignment: Alignment.bottomLeft,
                                    child: Text(
                                      _problem.content,
                                      style: TextStyle(
                                        fontSize: ScreenUtil().setSp(46),
                                        color: Colors.black,
                                      ),
                                    ),
                                  ),
                                  SizedBox(height: ScreenUtil().setHeight(8)),
                                  Align(
                                    alignment: Alignment.bottomLeft,
                                      child: ImgContainer(
                                    imgUrl: _imgUrl,
                                  )),
                                  SizedBox(
                                    height: ScreenUtil().setHeight(40),
                                  ),
                                  Row(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      Expanded(
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.end,
                                          children: [
                                            IconButton(
                                                icon: Icon(
                                                  MyIcons.thumb_up_alt,
                                                  size: ScreenUtil().setSp(60),
                                                  color: AppColors.subtext,
                                                ),
                                                onPressed: () {}),
                                            Text(
                                              _problem.thumbsUpNum.toString(),
                                              style: TextStyle(
                                                fontSize:
                                                    ScreenUtil().setSp(40),
                                                color: AppColors.subtext,
                                              ),
                                            ),
                                            SizedBox(
                                                width:
                                                    ScreenUtil().setWidth(30)),
                                            IconButton(
                                              icon: Icon(
                                                MyIcons.remark,
                                                size: ScreenUtil().setSp(75),
                                                color: AppColors.subtext,
                                              ),
                                              onPressed: () {},
                                            ),
                                            Text(
                                              _problem.replyNum.toString(),
                                              style: TextStyle(
                                                fontSize:
                                                    ScreenUtil().setSp(40),
                                                color: AppColors.subtext,
                                              ),
                                            ),
                                            SizedBox(
                                                width:
                                                    ScreenUtil().setWidth(30)),
                                            IconButton(
                                              icon: Icon(
                                                Icons.share_outlined,
                                                size: ScreenUtil().setSp(75),
                                                color: AppColors.subtext,
                                              ),
                                              onPressed: () {},
                                            ),
                                          ],
                                        ),
                                      )
                                    ],
                                  )
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                      Divider(
                        color: AppColors.subtext,
                        height: ScreenUtil().setHeight(12),
                      ),
                      Container(
                        width: ScreenUtil().screenWidth,
                        height: ScreenUtil().setHeight(578) *
                            _replyList.data.length,
                        color: Colors.white,
                        padding: EdgeInsets.only(
                            left: ScreenUtil().setWidth(55),
                            right: ScreenUtil().setWidth(55)),
                        child: ListView.builder(
                          itemCount: _replyList.data.length,
                          itemBuilder: (BuildContext context, int index) {
                            return new ProblemReplyItem(
                              problem: _replyList.data[index],
                            );
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
            Positioned(
              bottom: 0,
              left: 0,
              child: Container(
                height: ScreenUtil().setHeight(146),
                width: ScreenUtil().screenWidth,
                decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border(
                      top: BorderSide(width: 1, color: AppColors.subtext)),
                ),
                padding: EdgeInsets.only(
                    left: ScreenUtil().setWidth(34),
                    right: ScreenUtil().setWidth(34)),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      height: ScreenUtil().setHeight(110),
                      width: ScreenUtil().setWidth(793),
                      child: TextField(
                        decoration: InputDecoration(
                            focusedBorder: UnderlineInputBorder(
                              borderSide: (BorderSide(
                                color: AppColors.primary,
                              )),
                            ),
                            border: OutlineInputBorder(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(6)),
                            )),
                      ),
                    ),
                    Container(
                      height: ScreenUtil().setHeight(110),
                      width: ScreenUtil().setWidth(180),
                      decoration: BoxDecoration(
                        color: AppColors.primary,
                        borderRadius: BorderRadius.all(Radius.circular(5)),
                      ),
                      child: Center(
                        child: Text(
                          '发表',
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(50),
                            color: Colors.white,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      );
    }
  }
}
